﻿/*
* 创建者：LIYINFAI
* 创建时间：2023/10/24 9:35:26
* 描述：
* --------------------------------------
* 修改说明：
* --------------------------------------
*/
using CommonLibrary;
using CommonLibrary.Forms;
using System;
using System.Drawing;

namespace BusinessLibrary
{
    /// <summary>
    /// 氦检仪类
    /// </summary>
    public class Helium : IPlugin
    {
        /// <summary>
        /// 描述信息
        /// </summary>
        public string Description => "氦检仪相关业务处理";
        /// <summary>
        /// 执行方法
        /// </summary>
        /// <param name="pluginParam"></param>
        /// <returns></returns>
        public string Excute(PluginParamEntity pluginParam)
        {
            string result = string.Empty;
            if (pluginParam != null
                && APPInstance.PLC != null
                && APPInstance.PLC.IsConnected)
            {
                string identity = pluginParam.Identity.ToUpper();
                switch (identity)
                {
                    case "HELIUM_BACKRATE": result = HeliumBackRate(pluginParam); break;
                    case "HELIUM_HELIUM": result = HeliumJudge(pluginParam); break;
                    case "HELIUM_STATUS": result = HeliumStatus(pluginParam); break;
                }
            }
            return result;
        }
        /// <summary>
        /// 返回一个窗口
        /// </summary>
        /// <param name="pluginParam"></param>
        /// <returns></returns>

        public HWFrmWithTitle GetForm(PluginParamEntity pluginParam)
        {
            return null;
        }
        /// <summary>
        /// 返回一个 Bitmap
        /// </summary>
        /// <returns></returns>
        public Bitmap GetBitmap()
        {
            return null;
        }
        /// <summary>
        /// HELIUM_STATUS 状态判定
        /// </summary>
        /// <param name="pluginParam"></param>
        /// <returns></returns>
        private string HeliumStatus(PluginParamEntity pluginParam)
        {
            string methodName = "状态判定";
            this.Info($"【{pluginParam.Tag}】开始 - {methodName}");
            if (pluginParam != null && APPInstance.PLC != null && APPInstance.PLC.IsConnected)
            {
                var heliumEntity = CommonHelper.GetJson(pluginParam.ParamString, new HeliumExcuteEntity());
                try
                {
                    if (heliumEntity != null && !string.IsNullOrWhiteSpace(heliumEntity.OtherAddress))
                    {
                        this.Info($"【{pluginParam.Tag}】{methodName} - 写PLC, 地址：{heliumEntity.OtherAddress}, 状态：{heliumEntity.Status}");
                        APPInstance.PLC.Write(heliumEntity.OtherAddress, heliumEntity.Status);
                    }
                    if (heliumEntity != null && heliumEntity.ReadPermitAddresses != null && heliumEntity.ReadPermitAddresses.Count > 0)
                    {
                        this.Info($"【{pluginParam.Tag}】{methodName} - 写PLC, 是否允许读取：{heliumEntity.AllowReadPermit}");
                        foreach (var address in heliumEntity.ReadPermitAddresses)
                        {
                            APPInstance.PLC.Write(address, heliumEntity.AllowReadPermit);
                        }
                    }
                }
                catch (Exception ex)
                {
                    this.Error(ex);
                }
            }
            this.Info($"【{pluginParam.Tag}】结束 - {methodName}");
            return string.Empty;
        }

        /// <summary>
        /// HELIUM_BACKRATE 背景漏率检测
        /// </summary>
        /// <param name="pluginParam"></param>
        /// <returns></returns>
        private string HeliumBackRate(PluginParamEntity pluginParam)
        {
            string result = string.Empty;
            string methodName = "背景漏率判定";
            this.Info($"【{pluginParam.Tag}】开始 - {methodName}");
            if (pluginParam != null && APPInstance.PLC != null && APPInstance.PLC.IsConnected)
            {
                var heliumConfig = HeliumConfig.Instance();
                var heliumEntity = CommonHelper.GetJson(pluginParam.ParamString, new HeliumExcuteEntity());
                heliumEntity.Result = !heliumEntity.IsNormal || heliumEntity.LeakRate > heliumConfig.BackgroundRate ? (short)ENGCode.NG_BACK_HELIUM : (short)ENGCode.OK;
                if (!string.IsNullOrWhiteSpace(heliumEntity.OtherAddress))
                {
                    try
                    {
                        HeliumBackRatePLCEntity entity = CommonHelper.GetJson<HeliumBackRatePLCEntity>(heliumEntity.OtherAddress, null);
                        if (entity != null)
                        {
                            this.Info($"【{pluginParam.Tag}】{methodName} - 写PLC, 结果：{heliumEntity.Result}");
                            APPInstance.PLC.Write(entity.BackgroudJudgeAddress, heliumEntity.Result);
                        }
                    }
                    catch (Exception ex)
                    {
                        this.Error(ex);
                    }
                }
                result = CommonHelper.GetJsonString(heliumEntity, Newtonsoft.Json.Formatting.None);
            }
            this.Info($"【{pluginParam.Tag}】结束 - {methodName}，返回结果：{result}");
            return result;
        }
        /// <summary>
        /// HELIUM_HELIUM 氦检
        /// </summary>
        /// <param name="pluginParam"></param>
        /// <returns></returns>
        private string HeliumJudge(PluginParamEntity pluginParam)
        {
            string result = string.Empty;
            string methodName = "氦检判定";
            this.Info($"【{pluginParam.Tag}】开始 - {methodName}");
            if (pluginParam != null && APPInstance.PLC != null && APPInstance.PLC.IsConnected)
            {
                var heliumConfig = HeliumConfig.Instance();
                var heliumEntity = CommonHelper.GetJson(pluginParam.ParamString, new HeliumExcuteEntity());
                bool isOkPress = !heliumEntity.IsNormal || heliumEntity.LeakPress > heliumConfig.LeakPressMax || heliumEntity.LeakPress < heliumConfig.LeakPressMin ? false : true;
                bool isOkRate = !heliumEntity.IsNormal || heliumEntity.LeakRate > heliumConfig.LeakRateMax || heliumEntity.LeakRate < heliumConfig.LeakRateMin ? false : true;
                heliumEntity.Result = isOkRate && isOkPress ? (short)ENGCode.OK : (short)ENGCode.NG_HELIUM;

                if (!string.IsNullOrWhiteSpace(heliumEntity.OtherAddress))
                {
                    try
                    {
                        HeliumCheckPLCEntity entity = CommonHelper.GetJson<HeliumCheckPLCEntity>(heliumEntity.OtherAddress, null);
                        if (entity != null)
                        {
                            this.Info($"【{pluginParam.Tag}】{methodName} - 写PLC, 漏率：{heliumEntity.LeakRate}，压力：{heliumEntity.LeakPress}，结果：{heliumEntity.Result}");
                            APPInstance.PLC.Write(entity.LeakValueAddress, heliumEntity.LeakRate);
                            APPInstance.PLC.Write(entity.PressValueAddress, heliumEntity.LeakPress);
                            APPInstance.PLC.Write(entity.HeliumJudgeAddress, heliumEntity.Result);
                            APPInstance.PLC.Write(entity.HeliumResultAddress, heliumEntity.Result);
                        }
                    }
                    catch (Exception ex)
                    {
                        this.Error(ex);
                    }
                }
                result = CommonHelper.GetJsonString(heliumEntity, Newtonsoft.Json.Formatting.None);
            }
            this.Info($"【{pluginParam.Tag}】结束 - {methodName}，返回结果：{result}");
            return result;
        }
    }
}
